package com.lions.client.view.gui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.lions.client.model.GPSFeed;
import com.lions.client.model.HTMLStorage;
import com.lions.client.model.Model;
import com.lions.client.view.audio.AudioOutput;
import com.lions.client.view.mappanel.MapPanel;
import com.lions.client.view.modulepanel.ModulePanel;
import com.lions.client.view.popup.MyPopupPanel;

/**
 * Menu bar for the main GUI.
 * 
 * @author Brad
 * 
 */
public class MyMenuBar extends MenuBar {

	/**
	 * Constructor that initializes the menu bar and its click handlers.
	 * 
	 * @param m
	 * @param moduleP
	 */
	public MyMenuBar(final Model m, final ModulePanel moduleP,
			final MapPanel mapP) {
		super();
		model = m;
		modulePanel = moduleP;
		mapPanel = mapP;
		feed = new GPSFeed(model.getModuleList(), moduleP, mapP);
		//
		gpsMenu = new MenuBar(true);
		buildGPSCommands();
		addItem("GPS", gpsMenu);
		//
		storageMenu = new MenuBar(true);
		buildStorageCommands();
		storageMenu.addItem("Save Current Modules", saveLocalData);
		storageMenu.addItem("Load Saved Modules", loadLocalData);
		storageMenu.addItem("Clear All Modules", clearLocalData);
		addItem("Storage", storageMenu);
		//
		musicMenu = new MenuBar(true);
		buildMusicCommands();
		musicMenu.addItem("Play Windows Song", windowsSong);
		addItem("Music", musicMenu);
	}

	/**
	 * Build the menu commands that will be fired by file menu items
	 */
	private void buildGPSCommands() {
		for (int i = 1; i <= 10; i++) {
			final int num = i;
			gpsData = new Command() {
				public void execute() {
					feed.update(num);
				}
			};
			gpsMenu.addItem(
					"Retrieve GPS Data #" + new Integer(num).toString(),
					gpsData);
		}

	}

	/**
	 * 
	 */
	private void buildStorageCommands() {
		loadLocalData = new Command() {
			@Override
			public void execute() {
				int before = model.getModuleList().size();
				new HTMLStorage().storageLoad(model.getModuleList());
				// Populates the module list from any storage.
				int after = model.getModuleList().size();
				new MyPopupPanel("Loaded", "Loaded " + (after - before)
						+ " Modules");
				//
				modulePanel.populate();
				// Updates the
				mapPanel.populate();
			}
		};

		saveLocalData = new Command() {
			@Override
			public void execute() {
				Button ok = new Button("OK");
				ok.setStyleName("buttonstyle");
				ok.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(final ClickEvent event) {
						new HTMLStorage().storageSaveAll(model);
						new AudioOutput().playStorageSaved();
						popup.hide();
						new MyPopupPanel("Saved", "Saved "
								+ new HTMLStorage().getStorageSize()
								+ " Modules");
					}
				});
				Button cancel = new Button("Cancel");
				cancel.setStyleName("buttonstyle");
				cancel.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(final ClickEvent event) {
						popup.hide();
					}
				});
				HorizontalPanel panel = new HorizontalPanel();
				panel.add(ok);
				panel.add(cancel);
				popup = new MyPopupPanel(panel, "Save",
						"Do You Really Want Overwrite Saved Memory?\n This Cannot Be Undone!");

			}
		};

		clearLocalData = new Command() {
			public void execute() {
				Button ok = new Button("OK");
				ok.setStyleName("buttonstyle");
				ok.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(final ClickEvent event) {
						new HTMLStorage().storageClear(model.getModuleList());
						modulePanel.clearAllPanels();
						new AudioOutput().playClearStorage();
						popup.hide();
					}
				});
				Button cancel = new Button("Cancel");
				cancel.setStyleName("buttonstyle");
				cancel.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(final ClickEvent event) {
						popup.hide();
					}
				});
				HorizontalPanel panel = new HorizontalPanel();
				panel.add(ok);
				panel.add(cancel);
				popup = new MyPopupPanel(panel, "Clear Memory",
						"Do You Really Want To Clear All Modules?\n This Cannot Be Undone!");
			}
		};
	}

	/**
	 * 
	 */
	private void buildMusicCommands() {
		windowsSong = new Command() {
			@Override
			public void execute() {
				new AudioOutput().playWindowsSong();
			}
		};
	}

	/**
	 * Comment.
	 */
	private String url;
	/**
	 * Comment.
	 */
	private MyPopupPanel popup;
	/**
	 * Comment.
	 */
	private Model model;
	/**
	 * Comment.
	 */
	private ModulePanel modulePanel;
	/**
	 * Comment.
	 */
	private MapPanel mapPanel;
	/**
	 * Comment.
	 */
	private MenuBar gpsMenu;
	/**
	 * Comment.
	 */
	private MenuBar storageMenu;
	/**
	 * Comment.
	 */
	private MenuBar musicMenu;
	/**
	 * Comment.
	 */
	private Command gpsData;
	/**
	 * Comment.
	 */
	private Command loadLocalData;
	/**
	 * Comment.
	 */
	private Command clearLocalData;
	/**
	 * Comment.
	 */
	private Command saveLocalData;
	/**
	 * Comment.
	 */
	private Command windowsSong;
	/**
	 * Comment.
	 */
	private GPSFeed feed;
}
